package com.example.lizhixin.object_three_team.Base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;


/**
 * 所在包名：com.suneee.ftx.weilian.activity
 * 描述：Activity基类
 * 作者：Dabin
 * 创建时间：2018/2/8
 * 修改人：
 * 修改时间：
 * 修改描述：
 */

public abstract class BaseActivity extends AppCompatActivity {
    private InputMethodManager imm;
    protected Activity mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(setLayoutId());
        //绑定控件

        //view与数据绑定
        initView();
        //初始化数据
        initData();
        //设置监听
        setListener();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected abstract int setLayoutId();

    protected void initData() {

    }

    protected void initView() {

    }

    protected void setListener() {
    }

    public void finish() {
        super.finish();
        hideSoftKeyBoard();
    }

    //获取view的焦点 Activity中获取焦点 ，否则需求先获取activity 的 Context   主要解决关闭键盘
    public void hideSoftKeyBoard() {
        View localView = getCurrentFocus();
        if (this.imm == null) {
            this.imm = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        }
        if ((localView != null) && (this.imm != null)) {
            this.imm.hideSoftInputFromWindow(localView.getWindowToken(), 2);
        }
    }

    public void startActivity(Class clazz, boolean isFinish) {
        startActivity(new Intent(this, clazz));
        if (isFinish) {
            //销毁
            finish();
        }
    }

    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
